# Maintainer: Michael Hansen <zrax0111 gmail com>

_realname=unicorn
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-unicorn"
         "${MINGW_PACKAGE_PREFIX}-python2-unicorn"
         "${MINGW_PACKAGE_PREFIX}-python3-unicorn")
pkgver=1.0.1
pkgrel=5
pkgdesc="A lightweight multi-platform, multi-architecture CPU emulator framework based on QEMU (mingw-w64)"
url='https://www.unicorn-engine.org/index.html'
arch=('any')
license=('GPL2')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-python2"
             "${MINGW_PACKAGE_PREFIX}-python3"
             "${MINGW_PACKAGE_PREFIX}-python2-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools")
source=("${_realname}-${pkgver}.tar.gz::https://github.com/unicorn-engine/unicorn/archive/${pkgver}.tar.gz"
        001-mingw.patch)
sha512sums=('edfe1f7bfbc1d20f5b62232057e194a937bc09db686ef2efadb33a54605029a53426432cdb2a29511385aacdb9343b3b3091af50a1909098d7cf6db3429eb966'
            '842b1451888ceaf758d3f00f0eb595a3afa0fa18930666b28073d3f42641c71d18f6c45158e80f1b20f61271b86abc6b8524259ac63359a236abf5ec9c059514')

prepare() {
  cd "${_realname}-${pkgver}"
  patch -p1 -i ${srcdir}/001-mingw.patch
  
  sed 's|-O3|-O2|g' -i Makefile qemu/configure
  sed 's|-g ||g' -i qemu/configure
  sed 's|UNICORN_DEBUG ?= yes|UNICORN_DEBUG ?= no|g' -i config.mk
  cp -ra bindings/python{,3}
  sed -r 's|(python)$|\13|' -i bindings/python3/*.py
}

build() {
  cd "${_realname}-${pkgver}"
  make clean

  UNICORN_QEMU_FLAGS="--python=${MINGW_PREFIX}/bin/python2" \
  make PREFIX="${MINGW_PREFIX}"

  (cd bindings
    python2 const_generator.py python
  )
  (cd bindings/python
    python2 setup.py build
  )
  (cd bindings/python3
    python3 setup.py build
  )
}

package_unicorn() {
  cd "${_realname}-${pkgver}"

  make DESTDIR="${pkgdir}" PREFIX="${MINGW_PREFIX}" install

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING_GLIB" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING_GLIB"
}

package_python2-unicorn() {
  depends=("${MINGW_PACKAGE_PREFIX}-python2" "${MINGW_PACKAGE_PREFIX}-unicorn")
  cd "${_realname}-${pkgver}/bindings/python"

  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  python2 setup.py install --root="${pkgdir}" --prefix="${MINGW_PREFIX}" \
    -O1 --skip-build

  install -Dm 644 sample* shellcode.py -t "${pkgdir}${MINGW_PREFIX}/share/doc/python2-${_realname}/samples"
}

package_python3-unicorn() {
  depends=("${MINGW_PACKAGE_PREFIX}-python3" "${MINGW_PACKAGE_PREFIX}-unicorn")
  cd "${_realname}-${pkgver}/bindings/python3"

  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  python3 setup.py install --root="${pkgdir}" --prefix="${MINGW_PREFIX}" \
    -O1 --skip-build

  install -Dm 644 sample* shellcode.py -t "${pkgdir}${MINGW_PREFIX}/share/doc/python3-${_realname}/samples"
}

package_mingw-w64-i686-unicorn()   { package_unicorn; }
package_mingw-w64-x86_64-unicorn() { package_unicorn; }
package_mingw-w64-i686-python2-unicorn()   { package_python2-unicorn; }
package_mingw-w64-x86_64-python2-unicorn() { package_python2-unicorn; }
package_mingw-w64-i686-python3-unicorn()   { package_python3-unicorn; }
package_mingw-w64-x86_64-python3-unicorn() { package_python3-unicorn; }
